package com.ucode.application.controller.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ucode.application.form.sys.DataRoleForm;
import com.ucode.application.form.sys.DataRoleModuleItem;
import com.ucode.sys.model.DataRoleModule;
import com.ucode.sys.service.DataRoleService;
import com.ucode.sys.vo.DataRoleVo;
import com.ucode.tool.base.ResponseResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags="数据权限->角色管理")
@RestController
@RequestMapping("/sys-dataRole")
@Validated
public class DataRoleController {

    @Autowired
    private DataRoleService dataRoleService;
    
    @PreAuthorize("hasAuthority('sys:dataRole:read') or hasRole('ADMIN')")
    @ApiOperation("根据条件分页查询列表")
    @GetMapping(value = "/list")
    public ResponseResult<Object> findPage(
            @ApiParam(value ="日期")  @RequestParam(value = "ss") Date ss,
            @ApiParam(value="页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value="每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        
        
        return ResponseResult.success();
    }
    
    @PreAuthorize("hasAuthority('sys:dataRole:read') or hasRole('ADMIN')")
    @ApiOperation(value="根据主键查询")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "String",paramType = "path")
    @GetMapping("/getById/{id}")
    public ResponseResult<Object> getById(@PathVariable(name="id",required=true) Long id){
        DataRoleVo dataRoleVo = dataRoleService.findVoById(id);
        return ResponseResult.success(dataRoleVo);
    }
    
    @PreAuthorize("hasAuthority('sys:dataRole:write') or hasRole('ADMIN')")
    @ApiOperation(value="创建")
    @PostMapping("/create")
    public ResponseResult<Object> create(@Validated @RequestBody DataRoleForm form) {
        
        int i = dataRoleService.insert(formBuildDataRoleVo(form, null));
        
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('sys:dataRole:write') or hasRole('ADMIN')")
    @ApiOperation(value="更新")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "String",paramType = "path")
    @PostMapping("/update/{id}")
    public ResponseResult<Object> update(@PathVariable(name="id",required=true) Long id,
            @Validated @RequestBody DataRoleForm form) {
        
        int i = dataRoleService.update(formBuildDataRoleVo(form, id));
        
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
    private DataRoleVo formBuildDataRoleVo(DataRoleForm form,Long id){
        DataRoleVo  dataRoleVo = new DataRoleVo();
        dataRoleVo.setId(id);
        dataRoleVo.setName(form.getName());
        dataRoleVo.setRemark(form.getRemark());
        dataRoleVo.setStatuz(form.getStatuz());
        
        List<DataRoleModuleItem> items = form.getItem();
        List<DataRoleModule> dataRoleModules = null;
        if(CollUtil.isNotEmpty(items)){
            dataRoleModules = new ArrayList<DataRoleModule>();
            for (DataRoleModuleItem dataRoleModuleItem : items) {
                DataRoleModule dataRoleModule = new DataRoleModule();
                dataRoleModule.setModuleCode(dataRoleModuleItem.getModuleCode());
                dataRoleModule.setPermissionType(dataRoleModuleItem.getPermissionType());
                if(CollUtil.isNotEmpty(dataRoleModuleItem.getDeptIds())){
                    dataRoleModule.setDeptIds(StrUtil.join(",", dataRoleModuleItem.getDeptIds()));
                }
                dataRoleModules.add(dataRoleModule);
            }
            dataRoleVo.setDataRoleModules(dataRoleModules);
            
        }
        return dataRoleVo;
    }
    
}
